package com.ruoyi.wechat.api;

import com.google.zxing.WriterException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wechat.domain.CLeaseorder;
import com.ruoyi.wechat.domain.Product;
import com.ruoyi.wechat.service.ICLeaseorderService;
import com.ruoyi.wechat.service.QRCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 租赁订单Controller
 * 
 * @author ruoyi
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/wechat/leaseorder")
public class WxLeaseorderController extends BaseController
{
    @Autowired
    private ICLeaseorderService cLeaseorderService;

    @Autowired
    private QRCodeService qrCodeService;

    /**
     * 查询租赁订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CLeaseorder cLeaseorder)
    {
        startPage();
        List<CLeaseorder> list = cLeaseorderService.selectCLeaseorderList(cLeaseorder);
        return getDataTable(list);
    }

    /**
     * 获取租赁订单详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(cLeaseorderService.selectCLeaseorderByOrderId(orderId));
    }

    /**
     * 根据用户ID查询租赁订单及关联产品订单信息
     * @return 包含租赁订单和关联产品订单信息的列表
     */
    @GetMapping("/getCLeaseorderByUserId")
    public AjaxResult  getCLeaseorderByUserId(CLeaseorder cLeaseorder) {
        return success(cLeaseorderService.getCLeaseorderByUserId(cLeaseorder.getUserId(), cLeaseorder.getOrderId()));
    }

    /**
     * 根据用户ID查询租赁订单
     * @param userId 用户ID
     * @return 包含租赁订单和关联产品订单信息的列表
     */
    @GetMapping("/getCLeaseorderByUserIdList/{userId}")
    public AjaxResult  getCLeaseorderByUserIdList(@PathVariable("userId") String userId) {
        return success(cLeaseorderService.getCLeaseorderByUserIdList(userId));
    }

    /**
     * 新增租赁订单
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CLeaseorder cLeaseorder)
    {
        return success(cLeaseorderService.insertProductOrder(cLeaseorder));
    }

    /**
     * 删除租赁订单
     */
    @Log(title = "租赁订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(cLeaseorderService.deleteCLeaseorderByOrderIds(orderIds));
    }

    /**
     * 取消租赁订单
     */
    @Log(title = "取消赁订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/orderId/{orderId}")
    public AjaxResult orderRemove(@PathVariable("orderId") String orderId)
    {
        return toAjax(cLeaseorderService.deleteCLeaseorderByOrderId(orderId));
    }

    /**
     * 取消租赁订单中的商品商品
     */
    @Log(title = "取消赁商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/product/{oId}")
    public AjaxResult remove(@PathVariable("oId") String oId)
    {
        return toAjax(cLeaseorderService.deleteCLeaseorderByoId(oId));
    }

    /**
     * 二维码图片接口
     * @param text
     * @param response
     * @throws IOException
     * @throws WriterException
     * 访问地址：http://localhost:8080/wechat/leaseorder/generateQRCode?text=888
     */
    @GetMapping("/generateQRCode")
    public void generateQRCode(@RequestParam String text, HttpServletResponse response) throws IOException, WriterException {
        qrCodeService.generateQRCode(text, response);
    }
}
